package com.example.a10928.weifriend.http.poll;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by Chuang on 2018/4/4.
 * 请声明此类的作用，如无声明则留null
 */
public class IntervalPolling extends PollingRequest {
    private int inteval;

    public IntervalPolling(int inteval, String tag, EventInterface eventInterface, PollingAction pollingAction) {
        super(tag, eventInterface, pollingAction);
        this.inteval = inteval;
    }

    @Override
    public Disposable execute(PollingManager manager) {
        return Observable.interval(0,inteval, TimeUnit.SECONDS).
            compose(manager.composeEvent(tag, eventInterface)).
            observeOn(AndroidSchedulers.mainThread()).
            doOnNext(new Consumer<Long>() {
                @Override
                public void accept(Long aLong) throws Exception {
//                        Timber.tag(Constants.TAG).d("emit interval polling, Tag = " + tag + ", num = " + aLong);
                }
            }).
            subscribe(new Consumer<Long>() {
                @Override
                public void accept(Long num) throws Exception {
                    if (null != pollingAction) {
                        pollingAction.doAction(num);
                    }
//                        Timber.tag(Constants.TAG).d("running interval polling, Tag = " + tag + ", num = " + num);
                }
            });

    }
}
